<template>
	<view>
		<view class="content">
			<view class="list">
				<view class="row" v-for="(row,index) in addressList" :key="index" @tap="select(row)">
					<view class="center">
						<view class="name-tel">
							<view class="name">{{row.consigneeName}}</view>
							<view class="tel">{{row.phone}}</view>

						</view>
						<view class="addressBox">

							<view class="default" v-if="row.isDefault=='1'">
								{{i18n.default}}
							</view>
							<view class="address">
								{{row.address}}
							</view>
						</view>
					</view>
					<view class="right">
						<view class="icon bianji" @tap.stop="edit(row)">

						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add">
			<view class="btn" @tap="add">
				<view class="icon tianjia"></view>{{i18n.newAddress}}
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		computed: {
			i18n() {
				// 倒入你需要的对象
				return this.$t('address')
			}
		},
		data() {
			return {
				userId: '',
				token: '',
				isSelect: false,
				addressList: []
			};
		},
		onShow() {
			uni.getStorage({
				key: 'delAddress',
				success: (e) => {
					let len = this.addressList.length;
					if (e.data.hasOwnProperty('id')) {
						for (let i = 0; i < len; i++) {
							if (this.addressList[i].id == e.data.id) {
								this.addressList.splice(i, 1);
								break;
							}
						}
					}
					uni.removeStorage({
						key: 'delAddress'
					})
				}
			})
			uni.getStorage({
				key: 'saveAddress',
				success: (e) => {
					let len = this.addressList.length;
					if (e.data.hasOwnProperty('id')) {
						// 修改操作
						for (let i = 0; i < len; i++) {
							if (this.addressList[i].id == e.data.id) {
								this.addressList.splice(i, 1, e.data);
								break;
							}
						}
					} else {
						// 新增操作
						let lastid = this.addressList[len - 1];
						lastid++;
						e.data.id = lastid;
						this.addressList.push(e.data);
					}
					uni.removeStorage({
						key: 'saveAddress'
					})
				}
			})
		},
		onLoad(e) {
			if (e.type == 'select') {
				this.isSelect = true;
			}
			const userId = uni.getStorageSync('userId');
			if (userId) {
				this.userId = userId;
				console.log(userId);
			}
			const token = uni.getStorageSync('token');
			if (token) {
				this.token = token;
				console.log(token);
			}
			this.getLanguage();
			this.getAddressList(userId, token);
		},
		methods: {
			getAddressList(userId, token) {
				uni.request({
					url: 'http://localhost:52019/v1/template001/consignee/list', //获取购物车列表
					data: {
						pageNum: 1,
						pageSize: 10,
						ecCustomerId: userId
					},
					method: 'GET',
					dataType: 'json',
					header: {
						'token': token,
						'content-type': 'application/json' //自定义请求头信息
					},
					success: (res) => {
						console.log(res.data.data);
						this.addressList = res.data.data.list;
					}
				});
			},
			getLanguage() {
				var that = this;
				console.log(that.$i18n.locale)
				if (that.$i18n.locale === 'zh-CN') {
					uni.setNavigationBarTitle({
						title: '地址管理'
					});
				} else if (that.$i18n.locale === 'en-US') {
					uni.setNavigationBarTitle({
						title: 'Address management'
					});
				}
			},
			edit(row) {
				uni.setStorage({
					key: 'address',
					data: row,
					success() {
						uni.navigateTo({
							url: "edit/edit?type=edit"
						})
					}
				});

			},
			add() {
				uni.navigateTo({
					url: "edit/edit?type=add"
				})
			},
			select(row) {
				//是否需要返回地址(从订单确认页跳过来选收货地址)
				if (!this.isSelect) {
					return;
				}
				uni.setStorage({
					key: 'selectAddress',
					data: row,
					success() {
						uni.navigateBack();
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	view {
		display: flex;
	}

	.icon {
		// &.bianji {
		// 	&:before{content:"\e61b";}
		// }
		// &.tianjia {
		// 	&:before{content:"\e81a";}
		// }
	}

	.content {
		padding-bottom: 100upx;
	}

	.add {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		justify-content: center;
		align-items: center;
		background-color: #fff;

		.btn {
			width: 90%;
			height: 80upx;
			border: 1upx solid #8b6945;
			border-radius: 10upx;
			text-align: center;
			color: #8b6945;
			justify-content: center;
			align-items: center;

			.icon {
				height: 80upx;
				color: #8b6945;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}

			font-size: 30upx;
		}
	}

	.list {
		flex-wrap: wrap;

		.row {
			width: 96%;
			padding: 20upx 3%;
			border-bottom: 1upx solid #eee;

			.left {
				width: 90upx;
				flex-shrink: 0;
				align-items: center;

				.head {
					width: 70upx;
					height: 70upx;
					background: linear-gradient(to right, #ccc, #aaa);
					color: #fff;
					justify-content: center;
					align-items: center;
					border-radius: 60upx;
					font-size: 35upx;
				}
			}

			.center {
				width: 100%;
				flex-wrap: wrap;

				.name-tel {
					width: 100%;
					align-items: baseline;

					.name {
						font-size: 34upx;
					}

					.tel {
						margin-left: auto;
						font-size: 24upx;
						color: #777;
					}

				}

				.addressBox {
					.default {
						font-size: 22upx;
						background-color: #cd343c;
						color: #fff;
						padding: 0 18upx;
						border-radius: 10upx;
						margin-right: 20upx;
						height: 36upx;
						line-height: 36upx;
						flex-shrink: 0;
					}

					.address {
						width: 100%;
						font-size: 24upx;
						align-items: baseline;
						color: #777;
					}
				}

			}

			.right {
				flex-shrink: 0;
				align-items: center;
				margin-left: 20upx;

				.icon {
					justify-content: center;
					align-items: center;
					width: 60upx;
					padding-left: 10upx;
					height: 60upx;
					border-left: solid 1upx #aaa;
					font-size: 40upx;
					color: #777;
				}
			}
		}
	}
</style>
